package com.mm.learn.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wdx
 * @date 2021年08月06日 13:58
 * cas ==>比较并交换
 * 原理：
 * 1.自旋锁
 * 2.UnSave
 * 缺点：
 * 开销大
 * ABA问题 ==>解决 AtomicStampedReference
 * 只能保证一个共享变量的原子操作
 *
 */
@Slf4j
public class CasTest {
    static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 1;i<2000;i++){
                count++;

            }
        });
        Thread t2 =new Thread(()->{
            for (int i = 1;i<2000;i++){
                count++;
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        log.debug("count的值是{}",count);
        Lock lock = new ReentrantLock();
        lock.lock();
    }
}
